package com.tanger.im.service.contacts;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanger.common.result.ActionResult;
import com.tanger.common.utils.BeanUtils;
import com.tanger.im.entity.ImUserContacts;
import com.tanger.im.entity.IMUserContactsVO;
import com.tanger.im.mapper.IMUserContactsRepository;
import com.tanger.im.service.address.IMUserAddressService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * @Description TODO
 * @Author Hchenbin
 * @Date 2023-12-25 15:41
 * @Version 1.0
 */
@Slf4j
@Service
public class IMUserContactsServiceImpl implements IMUserContactsService {

    @Autowired
    private IMUserContactsRepository repository;
    @Autowired
    private IMUserAddressService addressService;

    @Override
    public List<IMUserContactsVO> findAll(Long userId) {
        return Optional.ofNullable(repository.findAll(userId)).orElseGet(Collections::emptyList);
    }

    @Override
    public IMUserContactsVO findOne(Long id) {
        return Optional.ofNullable(repository.findOne(id))
                .map(this::toMap)
                .orElse(null);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ActionResult save(ImUserContacts contacts) {
        ImUserContacts uc = repository.selectOne(
                new QueryWrapper<ImUserContacts>()
                        .eq("crony_id",contacts.getCronyId()));
        contacts.setIsDel(0);
        contacts.setIsBlack(0);

        if (Objects.nonNull(uc)) {
            uc = BeanUtils.copyProperties(contacts,uc);
            repository.updateById(uc);
            return ActionResult.buildResultSuccess("OK");
        }
        contacts.setCreateTime(System.currentTimeMillis());
        repository.insert(contacts);
        return ActionResult.buildResultSuccess("OK");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ActionResult black(Long id, Integer isBlack) {
        ImUserContacts uc = Optional.ofNullable(repository.selectById(id))
                .orElseThrow(() -> new RuntimeException("好友信息不存在或已删除"));
        uc.setIsBlack(isBlack);
        repository.updateById(uc);
        return ActionResult.buildResultSuccess("OK");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ActionResult delete(Long id) {
        ImUserContacts uc = Optional.ofNullable(repository.selectById(id))
                .orElseThrow(() -> new RuntimeException("好友信息不存在或已删除"));
        uc.setIsDel(0);
        repository.updateById(uc);
        return ActionResult.buildResultSuccess("OK");
    }

    protected IMUserContactsVO toMap(IMUserContactsVO contacts) {
        contacts.setAddress(addressService.findOneByUserId(contacts.getUserId()));
        return contacts;
    }
}
